<template>
  <div class="content">
    <div class="content-left" style="position: relative;">
      <!-- <div style="position: absolute;z-index: 1;left: -320px;">
        <a-space direction="vertical" :size="30">
          <Ad :body-style="{ width: '240px', height: '135px', borderRadius: '10px' }"></Ad>
          <Ad :body-style="{ width: '240px', height: '135px', borderRadius: '10px' }"></Ad>
          <Ad :body-style="{ width: '240px', height: '135px', borderRadius: '10px' }"></Ad>
          <Ad :body-style="{ width: '240px', height: '135px', borderRadius: '10px' }"></Ad>
          <Ad :body-style="{ width: '240px', height: '135px', borderRadius: '10px' }"></Ad>
        </a-space>
      </div> -->
      <div class="left-search-item">
        <h4>商品分类</h4>
        <a-tree :tree-data="contentData.cData" :selected-keys="contentData.selectedKeys" @select="onSelect"
          style="min-height: 220px;">
        </a-tree>
      </div>
      <div class="left-search-item">
        <h4>热门标签</h4>
        <div class="tag-view tag-flex-view">
          <span class="tag" :class="{ 'tag-select': contentData.selectTagId === item.id }"
            v-for="item in contentData.tagData" :key="item.id" @click="clickTag(item.id)">{{ item.title }}</span>
        </div>
      </div>
    </div>
    <div class="content-right">
      <div class="top-select-view flex-view">
        <div class="order-view">
          <span class="title"></span>
          <a-tabs v-model:activeKey="contentData.selectTabIndex" @tabClick="selectTab">
            <a-tab-pane :key="index" v-for="(item, index) in contentData.tabData">
              <template #tab="">
                <span v-if="!item.sort">{{ item.value }}</span>
                <a-dropdown v-else>
                  <span>
                    {{ item.value }}
                    <component :is="item.sort === 'asc' ? UpOutlined : DownOutlined" style="font-size: 10px;">
                    </component>
                  </span>
                  <template #overlay>
                    <a-menu @click="(menu) => {
                      clickTab(menu, index)
                    }">
                      <a-menu-item key="asc">
                        <a href="javascript:;">{{ item.value }} 从低到高</a>
                      </a-menu-item>
                      <a-menu-item key="desc">
                        <a href="javascript:;">{{ item.value }} 从高到低</a>
                      </a-menu-item>
                    </a-menu>
                  </template>
                </a-dropdown>
              </template>
            </a-tab-pane>
          </a-tabs>
        </div>
      </div>
      <a-spin :spinning="contentData.loading" style="min-height: 200px;">
        <div class="pc-thing-list flex-view">
          <div v-for="item in contentData.thingData" :key="item.id" @click="handleDetail(item)"
            class="thing-item item-column-3">
            <div class="img-view">
              <img :src="item.cover">
            </div>
            <span class="thing-name">{{ item.title }}</span>
            <span>
              <span class="a-price-symbol">¥</span>
              <span class="a-price">{{ item.price }}</span>
              <span class="a-orderCount" v-show="item.orderCount > 0">
                {{ item.orderCount > 999 ? '999+' : item.orderCount }}人下单
              </span>
            </span>
            <a-button type="primary">
              立即购买
            </a-button>
          </div>
          <div v-if="contentData.thingData.length <= 0 && !contentData.loading" class="no-data" style="">暂无数据</div>
        </div>
      </a-spin>
      <div class="page-view">
        <a-pagination v-model:current="contentData.page" size="small" @change="pageChange" :hideOnSinglePage="true"
          :defaultPageSize="contentData.pageSize" :total="contentData.total" :showTotal="(total) => `共${total}条数据`"
          :showSizeChanger="false" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { DownOutlined, UpOutlined } from '@ant-design/icons-vue';
import { listApi as listClassificationList } from '/@/api/classification'
import Ad from "./ad.vue";
import { listApi as listTagList } from '/@/api/tag'
import { listApi as listThingList } from '/@/api/thing'
import { BASE_URL } from "/@/store/constants";
const router = useRouter();
const contentData = reactive({
  selectX: 0,
  selectTagId: -1,
  cData: <any>[],
  selectedKeys: [],
  tagData: <any>[],
  loading: false,
  tabData: [{
    key: 'recent',
    value: '最新'
  }, {
    key: 'hot',
    value: '最热'
  }, {
    key: 'recommend',
    value: '推荐'
  }, {
    key: 'price',
    value: '价格',
    sort: 'asc',
  }, {
    key: 'order',
    value: '销量',
    sort: 'asc',
  }],
  selectTabIndex: 0,
  thingData: <any>[],
  page: 1,
  total: 0,
  pageSize: 12,
})
onMounted(() => {
  initSider()
  getThingList()
})
const initSider = () => {
  contentData.cData.push({ key: '-1', title: '全部' })
  listClassificationList({}).then(res => {
    res.data.forEach(item => {
      item.key = item.id
      contentData.cData.push(item)
    })
  })
  listTagList({}).then(res => {
    contentData.tagData = res.data
  })
}

const getSelectedKey = () => {
  if (contentData.selectedKeys.length > 0) {
    return contentData.selectedKeys[0]
  } else {
    return -1
  }
}
const onSelect = (selectedKeys) => {
  contentData.selectedKeys = selectedKeys
  getThingList();
}
const clickTag = (index) => {
  // if()
  contentData.selectTagId = contentData.selectTagId == index ? -1 : index;
  getThingList()
}

// 最新|最热|推荐
const selectTab = (index) => {
  contentData.selectTabIndex = index;
  contentData.page = 1;
  getThingList()
}
const clickTab = (menu, index) => {
  contentData.selectTabIndex = index;
  let tab = contentData.tabData[index];
  tab['sort'] = menu.key;
  contentData.page = 1;
  getThingList();
}
const handleDetail = (item) => {
  // 跳转新页面
  let text = router.resolve({ name: 'detail', query: { id: item.id } })
  window.open(text.href, '_blank')
}
const pageChange = (page, pageSize) => {
  contentData.page = page;
  contentData.pageSize = pageSize;
  getThingList();
}
const getThingList = () => {
  let tab = contentData.tabData[contentData.selectTabIndex];
  const data = {
    sort: tab.sort,
    filed: tab.key,
    c: getSelectedKey(),
    tag: contentData.selectTagId,
    page: contentData.page,
    pageSize: contentData.pageSize,
  };
  contentData.loading = true
  listThingList(data).then(res => {
    contentData.loading = false;
    contentData.thingData = res.data;
    contentData.total = res.total;
    contentData.thingData.forEach((item, index) => {
      if (item.cover) {
        item.cover = BASE_URL + '/api/staticfiles/image/' + item.cover
      }
    })
  }).catch(err => {
    contentData.loading = false
  })
}


</script>

<style scoped lang="less">
.content {
  display: flex;
  flex-direction: row;
  width: 1100px;
  margin: 80px auto;
}

.content-left {
  width: 220px;
  margin-right: 32px;
}

.left-search-item {
  overflow: hidden;
  border-bottom: 1px solid #cedce4;
  margin-top: 24px;
  padding-bottom: 24px;
}

h4 {
  color: #4d4d4d;
  font-weight: 600;
  font-size: 16px;
  line-height: 24px;
  height: 24px;
}

.category-item {
  cursor: pointer;
  color: #333;
  margin: 12px 0 0;
  padding-left: 16px;
}

ul {
  margin: 0;
  padding: 0;
}

ul {
  list-style-type: none;
}

li {
  margin: 4px 0 0;
  display: list-item;
  text-align: -webkit-match-parent;
}

.child {
  color: #333;
  padding-left: 16px;
}

.child:hover {
  color: #4684e2;
}

.select {
  color: #4684e2;
}

.flex-view {
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  //justify-content: space-between;
  display: flex;
}

.name {
  font-size: 14px;
}

.name:hover {
  color: #4684e2;
}

.count {
  font-size: 14px;
  color: #999;
}

.check-item {
  font-size: 0;
  height: 18px;
  line-height: 12px;
  margin: 12px 0 0;
  color: #333;
  cursor: pointer;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.check-item input {
  cursor: pointer;
}

.check-item label {
  font-size: 14px;
  margin-left: 12px;
  cursor: pointer;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.tag-view {
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-top: 4px;
}

.tag-flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.tag {
  background: #fff;
  border: 1px solid #a1adc6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-radius: 16px;
  height: 20px;
  line-height: 18px;
  padding: 0 8px;
  margin: 8px 8px 0 0;
  cursor: pointer;
  font-size: 12px;
  color: #152833;
}

.tag:hover {
  background: #4684e3;
  color: #fff;
  border: 1px solid #4684e3;
}

.tag-select {
  background: #4684e3;
  color: #fff;
  border: 1px solid #4684e3;
}

.content-right {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  padding-top: 12px;

  .pc-search-view {
    margin: 0 0 24px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;

    .search-icon {
      width: 20px;
      height: 20px;
      -webkit-box-flex: 0;
      -ms-flex: 0 0 20px;
      flex: 0 0 20px;
      margin-right: 16px;
    }

    input {
      outline: none;
      border: 0px;
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
      border-bottom: 1px solid #cedce4;
      color: #152844;
      font-size: 14px;
      height: 22px;
      line-height: 22px;
      -ms-flex-item-align: end;
      align-self: flex-end;
      padding-bottom: 8px;
    }

    .clear-search-icon {
      position: relative;
      left: -20px;
      cursor: pointer;
    }

    button {
      outline: none;
      border: none;
      font-size: 14px;
      color: #fff;
      background: #288dda;
      border-radius: 32px;
      width: 88px;
      height: 32px;
      line-height: 32px;
      margin-left: 2px;
      cursor: pointer;
    }

    .float-count {
      color: #999;
      margin-left: 24px;
    }
  }

  .flex-view {
    display: flex;
  }

  .top-select-view {
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 40px;
    line-height: 40px;

    .type-view {
      position: relative;
      font-weight: 400;
      font-size: 18px;
      color: #5f77a6;

      .type-tab {
        margin-right: 32px;
        cursor: pointer;
      }

      .type-tab-select {
        color: #152844;
        font-weight: 600;
        font-size: 20px;
      }

      .tab-underline {
        position: absolute;
        bottom: 0;
        //left: 22px;
        width: 16px;
        height: 4px;
        background: #4684e2;
        -webkit-transition: left .3s;
        transition: left .3s;
      }
    }

    .order-view {
      position: relative;
      color: #6c6c6c;
      font-size: 14px;

      .title {
        margin-right: 8px;
      }

      .tab {
        color: #999;
        margin-right: 20px;
        cursor: pointer;
      }

      .tab-select {
        color: #152844;
      }

      .tab-underline {
        position: absolute;
        bottom: 0;
        left: 84px;
        width: 16px;
        height: 4px;
        background: #4684e2;
        -webkit-transition: left .3s;
        transition: left .3s;
      }
    }

  }

  .pc-thing-list {
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;

    .thing-item {
      min-width: 255px;
      max-width: 255px;
      width: 255px;
      position: relative;
      flex: 1;
      height: 350px;
      overflow: hidden;
      margin: 20px 20px 0px 0px;
      cursor: pointer;
      border: 1px solid rgba(1, 1, 1, .2);
      border-radius: 6px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 10px;
      transition: transform 0.4s ease-in-out;
      /* 添加过渡效果 */
      background: #fff;
      box-shadow: -3px -3px 5px 0 rgba(0, 0, 0, .1),3px 3px 5px 0 rgba(0, 0, 0, .1);

      &:hover {
        transform: translateY(-10px)
      }

      .img-view {
        height: 200px;

        img {
          height: 200px;
          width: 186px;
          margin: 0 auto;
          background-size: contain;
          object-fit: contain;
        }
      }

      .thing-name {
        line-height: 40px;
        color: #0F1111 !important;
        font-size: 15px !important;
        font-weight: 400 !important;
        font-style: normal !important;
        text-transform: none !important;
        text-decoration: none !important;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: normal;
      }

      .price {
        color: #ff7b31;
        font-size: 20px;
        line-height: 20px;
        margin-top: 4px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .translators {
        color: #6f6f6f;
        font-size: 12px;
        line-height: 14px;
        margin-top: 4px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .no-data {
      height: 200px;
      line-height: 200px;
      text-align: center;
      width: 100%;
      font-size: 16px;
      color: #152844;
    }
  }

  .page-view {
    width: 100%;
    text-align: center;
    margin-top: 48px;
  }
}

.a-price-symbol {
  top: -0.5em;
  font-size: 16px;
  color: #ff6200;
}

.a-price {
  color: #ff6200;
  font-size: 22px;
}

.a-orderCount {
  margin-left: 10px;
  font-size: 12px;
  color: gray;
}
</style>
